package com.hengpeng.itfints.controller.news;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hengpeng.common.exception.ServiceResult;
import com.hengpeng.common.persist.QueryResult;
import com.hengpeng.common.util.StringHelper;
import com.hengpeng.itfinbase.persist.Article;
import com.hengpeng.itfinbase.persist.ArticleCategory;
import com.hengpeng.itfints.config.ConfigUtil;
import com.hengpeng.itfints.controller.BaseController;
import com.hengpeng.itfints.tools.PageUtil;

@Controller
@RequestMapping("/article")
public class ArticleController extends BaseController
{

	@RequestMapping(value = "/findArticle")
	public String findArticleByGroupId(ModelMap map, HttpServletRequest request, Long gid, int size, String backurl)
	{
		if (validateObjectsNull(size))
		{
			return returnErrorPage(map);
		}
		List<Article> result = serviceFactory.getArticleService().findArticleListByGroupId(gid, size);
		map.put("result", result);
		if (!validateObjectsNullOrEmpty(backurl))
		{
			return backurl;
		}
		return "/page/news/gnews.jsp";
	}
	@RequestMapping(value = "/articlelist")
	public String list(ModelMap map, HttpServletRequest request, String curPage)
	{
//		int int_curPage = (!StringHelper.isEmpty(curPage) ? Integer.parseInt(curPage) : 1);
//		ServiceResult<List<Article>> serviceResult = serviceFactory.getArticleService().queryList(int_curPage, 10);
//		PageUtil pUtil = new PageUtil(10, serviceResult.getReturnListSize(), int_curPage);
//		map.put("dataList", serviceResult.getReturnValue());
//		map.put("PageUtil", pUtil);
		
		List<Article> xwrd = serviceFactory.getArticleService().findArticleListByGroupId(204l, 5);//新闻热点
		List<Article> gjjj = serviceFactory.getArticleService().findArticleListByGroupId(200l, 5);//国际借鉴
		List<Article> xmjx = serviceFactory.getArticleService().findArticleListByGroupId(201l, 5);//项目解析
		List<Article> hydt = serviceFactory.getArticleService().findArticleListByGroupId(203l, 5);//行业动态
		List<Article> zcyj = serviceFactory.getArticleService().findArticleListByGroupId(202l, 5);//政策研究
		List<Article> xspx = serviceFactory.getArticleService().findArticleListByGroupId(100l, 5);//学术培训
		List<Article> ptywdt = serviceFactory.getArticleService().findArticleListByGroupId(400l, 5);//平台业务动态
		
		List<Article> zxgg = serviceFactory.getArticleService().findArticleListByGroupId(1l, 1);//站内公告
		
		map.put("xwrd", xwrd);
		map.put("gjjj", gjjj);
		map.put("xmjx", xmjx);
		map.put("hydt", hydt);
		map.put("zcyj", zcyj);
		map.put("xspx", xspx);
		map.put("ptywdt", ptywdt);
		
		map.put("zxgg", zxgg);
		return "/page/news/pfcontent/infocenter.jsp";

	}
	@RequestMapping(value = "/morelist")
	public String morelist(ModelMap map, HttpServletRequest request,String curPage)
	{
		int int_curPage = (!StringHelper.isEmpty(curPage) ? Integer.parseInt(curPage) : 1);
		ServiceResult<List<Article>> serviceResult = serviceFactory.getArticleService().queryList(int_curPage, 10);
		PageUtil pUtil = new PageUtil(10, serviceResult.getReturnListSize(), int_curPage);
		map.put("dataList", serviceResult.getReturnValue());
		map.put("PageUtil", pUtil);
		return "/page/news/pfcontent/morelist.jsp";

	}
	/**
	 * <pre>
	 * 首页播报
	 * </pre>
	 * @param map
	 * @param request
	 * @param gid
	 * @param size
	 * @param backurl
	 * @return
	 */
	@RequestMapping(value = "/manBroadcast")
	public String manBroadcast(ModelMap map, HttpServletRequest request, int size)
	{
		if (validateObjectsNull(size))
		{
			return returnErrorPage(map);
		}
		if (ConfigUtil.getParameter("man.broadcast.gid") != null)
		{
			Long gid = Long.parseLong(ConfigUtil.getParameter("man.broadcast.gid"));
			List<Article> result = serviceFactory.getArticleService().findArticleListByGroupId(gid, size);
			map.put("result", result);
		}
		return "/page/include/index_broadcast.jsp";
	}

	/**
	 * <pre>
	 * 首页最新公告、首页理财问答
	 * </pre>
	 * @param map
	 * @param request
	 * @param gid
	 * @param size
	 * @param backurl
	 * @return
	 */
	@RequestMapping(value = "/manNews")
	public String manNews(ModelMap map, HttpServletRequest request)
	{
		if (ConfigUtil.getParameter("man.zxgg.gid") != null && ConfigUtil.getParameter("man.lcwd.gid") != null)
		{
			Long zxggGid = Long.parseLong(ConfigUtil.getParameter("man.zxgg.gid"));//最新公告
			Long lcwdGid = Long.parseLong(ConfigUtil.getParameter("man.lcwd.gid"));//理财问答
			int size = 6;
			List<Article> zxgg = serviceFactory.getArticleService().findArticleListByGroupId(zxggGid, size);
			List<Article> lcwd = serviceFactory.getArticleService().findArticleListByGroupId(lcwdGid, size);
			map.put("zxggList", zxgg);
			map.put("lcwdList", lcwd);
		}
		return "/page/include/index_news.jsp";
	}

	/**
	 * <pre>
	 * 文章详情
	 * </pre>
	 * @param articleId
	 * @param map
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/details")
	public String findArticleDetail(Long articleId, ModelMap map, HttpServletRequest request)
	{
		Article article = serviceFactory.getArticleService().getArticle(articleId);
		if (article == null)
		{
			return "/404.jsp";
		}
		map.put("article", article);

		List<ArticleCategory> list = serviceFactory.getArticleService().findArticleCategoryAll();
		map.put("categoryList", list);
		ArticleCategory category = serviceFactory.getPersistService().get(ArticleCategory.class,
				article.getCategory().getId());
		map.put("category", category);
		return "/page/news/pfcontent/index.jsp";
	}

	@RequestMapping(value = "/findArticleCategory")
	public String findArticleCategory(ModelMap map, Long categoryId, Integer size, Integer currentPage)
	{
		if (size == null)
		{
			size = 10;
		}
		if (currentPage == null)
		{
			currentPage = 1;
		}
		List<ArticleCategory> list = serviceFactory.getArticleService().findArticleCategoryAll();
		map.put("categoryList", list);
		if (list != null && list.size() > 0 && categoryId == null)
		{
			categoryId = list.get(0).getId();
		}
		QueryResult<Article> resul = serviceFactory.getArticleService().findArticleByCategoryId(categoryId, size,
				currentPage);
		map.put("list", resul.getResults());
		ArticleCategory category = serviceFactory.getPersistService().get(ArticleCategory.class, categoryId);
		map.put("category", category);
		return "/page/news/index.jsp";
	}
}
